//
//  CollectTableViewController.m
//  XIWenLeJian
//
//  Created by lanou3g on 15/11/17.
//  Copyright © 2015年 蓝足. All rights reserved.
//

#import "CollectTableViewController.h"
#import "CollectModel.h"
#import "CollectTableViewHeaderFooterView.h"
#import "FileHandle.h"
#import "VideoModel.h"
#import "VideosTableViewCell.h"
#import "UserInfo.h"
#import "VediosViewController.h"
#import "NewsDetailViewController.h"
#import "NewsModel.h"
#import "LongTableViewCell.h"
#import "NewsTableViewCell.h"
#import "ThreeImagesTableViewCell.h"
#import "TopImageTableViewCell.h"

@interface CollectTableViewController ()

@property (nonatomic, strong)NSMutableDictionary *receiveDic;
@property (nonatomic, strong)NSMutableArray *receiveArray;

@end

@implementation CollectTableViewController

- (NSMutableArray *)receiveArray
{
    if (!_receiveArray) {
        _receiveArray = [[NSMutableArray alloc]initWithCapacity:4];
    }
    return _receiveArray;
}

- (NSMutableDictionary *)receiveDic
{
    if (!_receiveDic) {
        _receiveDic = [NSMutableDictionary dictionaryWithContentsOfFile:[FileHandle fileHandle].userPlist];
    }
    return _receiveDic;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    // cell的线
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.title = @"我的收藏";
    self.tableView.sectionHeaderHeight = 50;
    [self loadData];
}

// 数据
- (void)loadData
{
    self.receiveDic = nil;
    self.receiveArray = nil;
    
// 如果你知道大概要放多少东西，那么最好用initWithCapacity,这个会提高程序内存运用效率
    NSMutableArray *array = [NSMutableArray array];
    [array addObjectsFromArray:[CollectModel collectWithDic:self.receiveDic]];
    [self.receiveArray addObjectsFromArray:[CollectModel collectWithDic:self.receiveDic]];
    for (int i = 0; i < 4; i++) {
        CollectModel *collectModel = array[i];
        if ([collectModel.name isEqualToString:@"news"]) {
            [self.receiveArray replaceObjectAtIndex:0 withObject:collectModel];
        }
        if ([collectModel.name isEqualToString:@"texts"]) {
            [self.receiveArray replaceObjectAtIndex:1 withObject:collectModel];
        }
        if ([collectModel.name isEqualToString:@"pictures"]) {
            [self.receiveArray replaceObjectAtIndex:2 withObject:collectModel];
        }
        if ([collectModel.name isEqualToString:@"videos"]) {
            [self.receiveArray replaceObjectAtIndex:3 withObject:collectModel];
        }
    }
}

// 设置区头
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    CollectTableViewHeaderFooterView *headerView = [CollectTableViewHeaderFooterView setHeaderView:tableView];
    headerView.collectModel = self.receiveArray[section];
    __weak typeof(self) weakSelf = self;
    headerView.collectBlock = ^(){
        [weakSelf.tableView reloadData];
    };
    return headerView;
}


#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return self.receiveArray.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    CollectModel *collectModel = self.receiveArray[section];
    return collectModel.isShow ? collectModel.content.count : 0;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    CollectModel *collectModel = self.receiveArray[indexPath.section];
    switch (indexPath.section) {
        case 0:{
            NewsModel *model = collectModel.content[indexPath.row];
            return [NewsTableViewCell addNewsCellWithTableView:tableView model:model];
        }
            break;
        case 1:{
            
        }
            break;
        case 2:{
            
        }
            break;
        case 3:{
            static NSString *videocell = @"videoCell";
            VideosTableViewCell *videoCell = [tableView dequeueReusableCellWithIdentifier:videocell];
            if (videoCell == nil) {
                videoCell = [[VideosTableViewCell alloc]initWithStyle:(UITableViewCellStyleSubtitle) reuseIdentifier:videocell];
            }
            videoCell.model = collectModel.content[indexPath.row];
            
            // 播放
            __weak typeof(self) weakSelf = self;
            videoCell.movieBlock = ^(NSString *str){
                NSURL *url = [NSURL URLWithString:str];
                weakSelf.playerVC = [[MPMoviePlayerViewController alloc]initWithContentURL:url];
                [weakSelf.playerVC.moviePlayer setScalingMode:(MPMovieScalingModeNone)];
                [weakSelf.playerVC.moviePlayer setControlStyle:(MPMovieControlStyleFullscreen)];
                [weakSelf.playerVC.moviePlayer.backgroundView setFrame:weakSelf.view.bounds];
                [weakSelf.playerVC.moviePlayer.backgroundView setBackgroundColor:[UIColor colorWithRed:1 green:1 blue:1 alpha:0.1]];
                
                // 注册一个播放结束的通知
                [[NSNotificationCenter defaultCenter]addObserver:weakSelf selector:@selector(end:) name:MPMoviePlayerPlaybackDidFinishNotification object:nil];
                // 弹出模态窗口  全屏
                [weakSelf presentMoviePlayerViewControllerAnimated:weakSelf.playerVC];
                // 用play方法 电影播放控制器会自动将视图切换到电影播放器并开始播放
                [weakSelf.playerVC.moviePlayer play];
            };
            videoCell.reloadDataBlock = ^{
                [weakSelf.tableView reloadData];
            };
            return videoCell;
        }
        default:
            break;
    }
    return nil;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    switch (indexPath.section) {
        case 0:{
            return 100;
        }
            break;
        case 1:{
            return 100;
        }
            break;
        case 2:{
            return 100;
        }
            break;
        case 3:{
            CollectModel *collectModel = self.receiveArray[indexPath.section];
            VideoModel *model = collectModel.content[indexPath.row];
            int height = [model.height intValue];
            int width = [model.width intValue];
            if (height * ([UIScreen mainScreen].bounds.size.width - 100) / (width + 1) >= ([UIScreen mainScreen].bounds.size.width - 25)) {
                return (height * ([UIScreen mainScreen].bounds.size.width - 100) / (width + 1)) + 70;
            }else{
                return (height * ([UIScreen mainScreen].bounds.size.width - 40) / (width + 1)) + 70;
            }
        }
            break;
        default:
            break;
    }
    return 0;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    //选中后取消选中， 不会变色
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    if (indexPath.section != 0) {
        return;
}
    NewsDetailViewController *NewsDetailVC = [[NewsDetailViewController alloc] init];
    CollectModel *collect = self.receiveArray[indexPath.section];
    NewsModel *model = collect.content[indexPath.row];
    // 本地 plist 存在即为收藏
    if ([UserInfo shareUserInfo].isLogin) {
        if ([[FileHandle fileHandle]foundNewsForID:model.docid]) {
            model.isCollect = YES;
        }
    }
    // 传值推出下一界面
    NewsDetailVC.newsModel = model;
    [self.navigationController pushViewController:NewsDetailVC animated:YES];
}

- (void)end:(id)sender
{
    // 移除观察者
    [[NSNotificationCenter defaultCenter]removeObserver:self name:MPMoviePlayerPlaybackDidFinishNotification object:nil];
    [self.playerVC.view removeFromSuperview];
    // 释放资源
    self.playerVC = nil;
}



@end
